'''
    a3mall业务
'''
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
# 显示等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains

class Base():
    # 初始化
    def __init__(self,driver):
        self.driver = driver
    # 封装网址
    def open_url(self,url):
        self.driver.get(url)
    # 时间戳
    def get_time(self):
        em = time.strftime('%Y%m%d_%H%M%S',time.localtime())
        self.driver.save_screenshot('../Png'+'Img'+em+'.png')
    # 封装定位方法，使用显示等待，如果超过20秒后截图
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,1).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            self.get_time()
    # 封装点击事件 鼠标事件
    def click(self,loc):
        self.find_element(loc).click()
    # 输入事件 send_keys
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 回车事件 键盘事件
    def enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # 输出文本
    def text(self,loc):
        return self.find_element(loc).text
    # iframe处理 进入
    def frame(self):
        self.driver.switch_to.frame()
    # iframe处理 退出
    def frame_uncon(self):
        self.driver.switch_to.default_content()
    # 弹框处理
    def accept(self,loc):
        alert = self.driver.switch_to.alert(self.find_element(loc))
        alert.accept()
    def dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()
    # 滚动条
    def scrollTo(self):
        self.driver.execute_script('window.scrollTo(0,800)')
    # 切换页面
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 浏览器事件
    def refresh(self):
        self.driver.refresh()
    # 下拉框选择
    def Select(self,loc,x):
        a = Select(self.find_element(loc))
        a.select_by_index(x)

